/**
 * Created by Administrator on 2017/6/12.
 */

var add=document.querySelector(".add");//添加数据按钮
var hidden=document.querySelector(".hidden");//全屏蒙版
var addInfore=document.querySelector(".addInfore");//弹出的添加信息框
var inputlist=addInfore.getElementsByTagName("input");//获取所有输入框
var ok=document.querySelector(".ok");//添加按钮
var tables=document.querySelector(".tables");//获取table表格
var cancel=document.querySelector(".cancel");//注销按钮
var close=document.querySelector(".close");//关闭按钮
var refresh=document.querySelector(".refresh");//刷新按钮

var baseUrl="http://localhost:6500/books";//服务端的跟路径
var xhr=cohenLee.createXHR();//创建XHR对象
/*获取后台数据*/
xhr.open("get", baseUrl);
xhr.send(null);
xhr.onreadystatechange = function(){
    if(xhr.readyState == 4){
        if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
            createList(JSON.parse(xhr.responseText));
        }
    }
};
/*创建表格，并把后台获取的数据一一对应添加*/
function createList(data) {
    for(var i=0;i<data.length;i++){
    var tr=document.createElement("tr");
    var td1=document.createElement("td");
    var td2=document.createElement("td");
    var td3=document.createElement("td");
    var td4=document.createElement("td");
    var td5=document.createElement("td");
    var td6=document.createElement("td");
    var td7=document.createElement("td");
    var check=document.createElement("input");
    var img=document.createElement("img");
    img.style.width="30px";
    check.setAttribute("type","checkbox");
    tr.appendChild(td1);
    tr.appendChild(td2);
    tr.appendChild(td3);
    tr.appendChild(td4);
    tr.appendChild(td5);
    tr.appendChild(td6);
    tr.appendChild(td7);
    tables.appendChild(tr);
    td1.appendChild(check);
    td2.appendChild(img);
        img.src=data[i]["bookImage"];
        td3.textContent=data[i]["bookName"];
        td4.textContent=data[i]["bookAuthor"];
        td5.textContent=data[i]["VIP"];
        td6.textContent=data[i]["bookPublish"];
        var a1=document.createElement("a");
        td7.appendChild(a1);
        a1.innerHTML="删除";
        a1.setAttribute('data-id',data[i]['id']);//给他添加一个id属性
        var a2=document.createElement("a");
        td7.appendChild(a2);
        a2.innerHTML="修改";
        a2.style.marginLeft="8px";
        a2.setAttribute('data-id',data[i]['id']);//给他添加一个id属性
        a2.onclick = updateHandle;
        a1.onclick = deleteHandle;
    }
}
/*添加数据按钮*/
add.onclick=function (e) {
    if(!e){
        e=window.event;
    }
    hidden.style.display="block";
};
/*关闭按钮*/
close.onclick=function () {
    hidden.style.display="none";
};
/*添加按钮*/
ok.onclick=function (e) {
    if (!e) {
        e = window.event;
    }
    for (var i = 0; i < inputlist.length; i++) {
        if (inputlist[i].value == "") {
            window.alert("请将信息填写完整");
            return;
        }
    }
    xhr.open("post", baseUrl);
    xhr.setRequestHeader("Content-type", "application/json");
    var obj = {};
    for (var j = 0; j < inputlist.length; j++) {
        obj[inputlist[j].getAttribute("name")] = inputlist[j].value;
    }
    xhr.send(JSON.stringify(obj));
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
            }
        }
    }
};
//修改
function updateHandle() {
    hidden.style.display="block";
    var id=this.getAttribute("data-id");
    xhr.open("get",baseUrl+"/"+id);
    xhr.send(null);
    var t=this;
    ok.setAttribute("data-id",id);
    ok.onclick=updateHandles;
    xhr.onreadystatechange = function(){
            if(xhr.readyState == 4){
                if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
                    var data = null;
                    data = JSON.parse(xhr.responseText);
                    for(var a=0;a<inputlist.length;a++){
                        var atName = inputlist[a].getAttribute("name");
                        inputlist[a].setAttribute("value", data[atName]);
                    }
                }
            }
}
}
/*把修改后的数据再次添加*/
function updateHandles(){
    xhr.open("put", baseUrl + "/" + this.getAttribute("data-id"));
    xhr.setRequestHeader("Content-Type","application/json");
    var obj = {};
    obj.id = this.getAttribute("data-id");
    for(var i = 0; i < inputlist.length; i++){
        obj[inputlist[i].getAttribute("name")] = inputlist[i].value;
    }
    xhr.send(JSON.stringify(obj));
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4){
            if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
                hidden.style.display = 'none';
            }
        }
    };
}
//删除事件
function deleteHandle() {
    xhr.open("delete", baseUrl + "/" + this.getAttribute('data-id'));
    xhr.setRequestHeader("Content-Type","application/json");	//设置请求传递数据的类型
    var obj = {};
    obj.id = this.getAttribute('data-id');
    xhr.send(JSON.stringify(obj));
    var t = this;
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4){
            if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
                t.parentNode.parentNode.remove();
                console.log(t)
            }
        }
    };
}

/*获取本地存储里的用户名和密码*/
var data=JSON.parse(localStorage.getItem("a"));

/*点击头像，出现框*/
$(".headPic").click(function () {
    $(".zhuxiao").toggle("normal");
  for(var i in data){
      $(".getName").html(data.user);
      $(".getPws").html(data.pwd);
  }
});
/*点击注销按钮，重新登录*/
cancel.onclick=function () {
    xhr.open("post","http://localhost:6500/users/logout");
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.send(null);
    xhr.onreadystatechange=function () {
        if(xhr.readyState==4){
            if(xhr.status>=200&&xhr.status<300||xhr.status==304){
                window.location.href="system.html";
            }
        }
    };
};
//点击刷新按钮，刷新当前页面
refresh.onclick=function () {
    window.location.reload();
};

